<!--
 - MineAdmin is committed to providing solutions for quickly building web applications
 - Please view the LICENSE file that was distributed with this source code,
 - For the full copyright and license information.
 - Thank you very much for using MineAdmin.
 -
 - @Author X.Mo<root@imoi.cn> and NEKGod<1559096467@qq.com>
 - @Link   https://github.com/mineadmin
-->
<script setup lang="ts">
import type { MaFormExpose } from '@mineadmin/form'
import useForm from '@/hooks/useForm'
import { ResultCode } from '@/utils/ResultCode'
import { deliver } from '$/attractions/commerce/api/commerceOrder'

defineOptions({ name: 'commerce_order:commerceOrder:deliverForm' })

const { data = null } = defineProps<{
  data?: any | null
}>()

const t = useTrans().globalTrans
const formRef = ref<MaFormExpose>()
const formData = ref<any>({
  id: data?.id || '',
  express_company: '',
  delivery_time: new Date().toISOString().slice(0, 19),
  express_id: '',
  express_cost: 0
})

useForm('formRef').then((form: MaFormExpose) => {
  form.setItems([
    {
      prop: 'express_company',
      label: '快递公司',
      render: 'input',
      renderProps: {
        placeholder: '请输入快递公司名称，如：顺丰、圆通、中通等'
      },
      itemProps: {
        rules: [{ required: true, message: '请输入快递公司名称' }]
      }
    },
    {
      prop: 'delivery_time',
      label: '发货时间',
      render: 'DatePicker',
      renderProps: {
        type: 'datetime',
        placeholder: '请选择发货时间',
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      },
      itemProps: {
        rules: [{ required: true, message: '请选择发货时间' }]
      }
    },
    {
      prop: 'express_id',
      label: '快递单号',
      render: 'input',
      renderProps: {
        placeholder: '请输入快递单号'
      },
      itemProps: {
        rules: [{ required: true, message: '请输入快递单号' }]
      }
    },
    {
      prop: 'express_cost',
      label: '快递费用',
      render: 'InputNumber',
      renderProps: {
        placeholder: '请输入快递费用（元）',
        min: 0,
        precision: 2,
        step: 0.01,
        controlsPosition: 'right'
      },
      itemProps: {
        rules: [{ required: true, message: '请输入快递费用' }]
      }
    }
  ])
  form.setOptions({
    labelWidth: '120px',
  })
})

// 发货操作
function deliverOrder(): Promise<any> {
  return new Promise((resolve, reject) => {
    // 只传递发货相关的字段
    const deliverData = {
      express_company: formData.value.express_company,
      delivery_time: formData.value.delivery_time,
      express_id: formData.value.express_id,
      express_cost: formData.value.express_cost,
    }
    
    deliver(formData.value.id, deliverData).then((res: any) => {
      res.code === ResultCode.SUCCESS ? resolve(res) : reject(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

defineExpose({
  deliverOrder,
  maForm: formRef
})
</script>

<template>
  <div>
    <el-alert
      title="发货提示"
      type="info"
      :closable="false"
      show-icon
      style="margin-bottom: 20px;"
    >
      请填写准确的快递信息，发货后订单状态将变更为"待收货"，客户将收到发货通知。
    </el-alert>
    <ma-form ref="formRef" v-model="formData" />
  </div>
</template>

<style scoped lang="scss">

</style> 